//
//  BCLanguageSettingController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/29.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCLanguageSettingController.h"

//cell
#import "BCLanguageSettingCell.h"

@interface BCLanguageSettingController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *languageTable;

/**
 *languageSource
 */
@property (nonatomic, copy) NSMutableArray *languageSource;

@end

@implementation BCLanguageSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setupSubviews];
    //
    [self reloadData];
}

- (void)setupSubviews {
    self.languageTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.languageTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.languageTable.dataSource = self;
    self.languageTable.delegate = self;
    [self.view addSubview:self.languageTable];
    [self.languageTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    //
    [self.languageTable registerClass:BCLanguageSettingCell.class forCellReuseIdentifier:NSStringFromClass(BCLanguageSettingCell.class)];
}

/**
 reloadData
 */
- (void)reloadData {
    self.title = BCLocalizedKey(@"Language Setting");
    //
    self.languageSource = [LanguageSettingCellModel languageSource];
    [self.languageTable reloadData];
}


#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.languageSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCLanguageSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(BCLanguageSettingCell.class)];
    cell.cellModel = self.languageSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [BCLanguageSettingCell defaultHeight];
}
//
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LanguageSettingCellModel *model = self.languageSource[indexPath.row];
    if (!model.isCurrent) {
        [self toChangeLanguageWithModel:model];
    }
}

/**
 更改语言
 */
- (void)toChangeLanguageWithModel:(LanguageSettingCellModel *)model {
    WEAK_SELF
    NSString *title = BCLocalizedFormatString(@"Would you like to change the language to %@?",model.localLanguageString);
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //
    NSString *changeTitle = BCLocalizedFormatString(@"Change to %@",model.localLanguageString);
    UIAlertAction *changeAction = [UIAlertAction actionWithTitle:changeTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [wself sureToChangeLanguageToType:model.languageType];
    }];
    [actionSheet addAction:changeAction];
    //
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:BCLocalizedKey(@"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [actionSheet addAction:cancelAction];
    //
    [self presentViewController:actionSheet animated:true completion:nil];
}
- (void)sureToChangeLanguageToType:(BCLanguageType)type {
    self.view.userInteractionEnabled = false;
    [BCLabguageSettingManager shared].appLanguage = type;
    UIView *tipsView = [UIApplication sharedApplication].keyWindow;
    NSString *tips = BCLocalizedKey(@"Setting Language");
    [QMUITips showLoading:tips inView:tipsView];
    //
    [[NSNotificationCenter defaultCenter] postNotificationName:UserSettingLanguageChangedNotification object:nil];
    //
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [QMUITips hideAllTipsInView:tipsView];
        //
        [self reloadData];
        //
        NSString *success = BCLocalizedKey(@"Setting Success");
        NSTimeInterval delayTime = [QMUITips smartDelaySecondsForTipsText:success];
        [QMUITips showSucceed:success inView:self.view hideAfterDelay:delayTime];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:true completion:nil];
        });
    });
}

@end
